﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace New3DDemo
{
    /// <summary>
    /// Road.xaml 的交互逻辑
    /// </summary>
    public partial class Road : UserControl
    {
        public Road()
        {
            InitializeComponent();
        }

        #region 注册依赖属性

        #region Offset1

        public double Offset1
        {
            get { return (double)GetValue(Offset1Property); }
            set { SetValue(Offset1Property, value); }
        }

        public static readonly DependencyProperty Offset1Property =
            DependencyProperty.Register("Offset1", typeof(double), typeof(Road), new UIPropertyMetadata(0.0));
        
        #endregion

        #region Offset2

        public double Offset2
        {
            get { return (double)GetValue(Offset2Property); }
            set { SetValue(Offset2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Offset2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Offset2Property =
            DependencyProperty.Register("Offset2", typeof(double), typeof(Road), new UIPropertyMetadata(0.0));

        #endregion

        #region OriginPoints

        public PointCollection OriginPoints
        {
            get { return (PointCollection)GetValue(OriginPointsProperty); }
            set { SetValue(OriginPointsProperty, value); }
        }

        public static readonly DependencyProperty OriginPointsProperty =
            DependencyProperty.Register("OriginPoints", typeof(PointCollection), typeof(Road), new UIPropertyMetadata(new PropertyChangedCallback(PropertyChanged)));
        
        #endregion

        #region MiddleLinePoints

        public PointCollection MiddleLinePoints
        {
            get { return (PointCollection)GetValue(MiddleLinePointsProperty); }
            set { SetValue(MiddleLinePointsProperty, value); }
        }

        public static readonly DependencyProperty MiddleLinePointsProperty =
            DependencyProperty.Register("MiddleLinePoints", typeof(PointCollection), typeof(Road), new UIPropertyMetadata(new PropertyChangedCallback(PropertyChanged)));
        
        #endregion

        #endregion

        #region 私有方法

        private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Road)d).PointsList_CollectionChanged();
        }

        void PointsList_CollectionChanged()
        {
            MiddleLinePoints = OriginPoints;
            this.pl1.Points = new PointCollection(LineHelper.MakeParallelLine(MiddleLinePoints.ToList(), 80 + 2.5 + 4));
            this.pl2.Points = new PointCollection(LineHelper.MakeParallelLine(MiddleLinePoints.ToList(), -80 - 2.5 - 4));

            this.Plroad.Points = Helper.ConcatReversePoints(this.pl1.Points, this.pl2.Points);
        } 
        #endregion
    }
}
